<template>
	<view class="page">
		<image src="/static/img/logoIndex.png" mode="scaleToFill" class="logo"></image>
		<button class="btn primary" open-type="getPhoneNumber" @getphonenumber="getWxUserPhone">一键授权微信手机号</button>
	</view>
</template>

<script>
	const app = getApp();
	import {
		updateUserInfo,
	} from '@/api/user.js';
	import {
		isEmpty
	} from '@/utils/util.js'

	export default {
		data() {
			return {
				options: null,
			};
		},
		onLoad(options) {
			let that = this;
			let pages = getCurrentPages();
			let prePage = pages[pages.length - 2];
		},

		methods: {

			// 绑定手机号
			getWxUserPhone({
				detail
			}) {
				let that = this
				if (detail.errMsg != 'getPhoneNumber:ok') {
					console.log('微信授权获取手机号失败', detail.errMsg)
					return
				}
				uni.showLoading()
				that.$store.dispatch('loginByWxPhone', detail.code).then(res => {
					that._updateUserInfo(res)
					uni.hideLoading()
				})
			},

			_updateUserInfo(phonenumber) {
				let that = this
				updateUserInfo({
					"phonenumber": phonenumber,
				}).then(res => {
					that.$util.showSuccess('登录成功')
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.page {
		padding: 32rpx;
	}

	.logo {
		display: block;
		width: 200rpx;
		height: 200rpx;
		margin-top: 120rpx;
		margin: 120rpx auto 0;
	}

	.btn {
		width: 100%;
		margin-top: 120rpx;
	}
</style>
